package leechapi.main;

import java.io.IOException;
import java.io.InputStream;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import static leechapi.main.Log.*;

public class Application {

	public Application() {
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		log("DEBUT");
		Application a = new Application();
		ProcessingContext pc = new ProcessingContext();
		// effacement
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String dateAsString = sdf.format(new Date());
		try {
			PreparedStatement deleteOffres = pc
					.getConnection()
					.prepareStatement(
							"delete from offre where date =  PARSEDATETIME(?,'yyyy-MM-dd')");
			deleteOffres.setString(1, dateAsString);
			deleteOffres.executeUpdate();
		} catch (SQLException e1) {
			log("Impossible d'effacer les offres pour la date " + dateAsString
					+ " : " + e1.getMessage());
			e1.printStackTrace();
		}
		// ouest france
		leechOuestFrance(pc);
		//leechImmonotBretagne(pc);
		// fin
		pc.close();
		log("FIN");
	}

	/**
	 * Scanne OuestFrance
	 * 
	 * @param pc
	 * @param threads
	 * @throws IOException
	 */
	private static void leechOuestFrance(ProcessingContext pc) throws IOException {
		InputStream is = Application.class.getClassLoader()
				.getResourceAsStream("leechapi/main/url.properties");
		Properties props = new Properties();
		props.load(is);
		String[] requetes = ((String) props.get("of.requetes")).split(",");

		for (String requete : requetes) {
			OFLeecher leecher = new OFLeecher(((String) props.get(requete
					+ ".url")), ((String) props.get(requete + ".codepostal")),
					requete.substring(3, requete.length()), pc);
			List<Thread> threads = new ArrayList<Thread>();
			for (int i = 0; i < 10; i++) {
				Thread t = new Thread(leecher);
				t.setName("OFLeecher " + i);
				threads.add(t);
				t.start();
			}
			try {
				for (Thread t : threads) {
					t.join();
				}
			} catch (InterruptedException e) {
				log(e.getMessage());
				e.printStackTrace();
			}
		}
	}

	/**
	 * Scanne ImmonotBretagne
	 * 
	 * @param pc
	 * @param threads
	 * @throws IOException
	 */
	private static void leechImmonotBretagne(ProcessingContext pc) throws IOException {
		InputStream is = Application.class.getClassLoader()
				.getResourceAsStream("leechapi/main/url.properties");
		Properties props = new Properties();
		props.load(is);
		String[] requetes = ((String) props.get("immonot.requetes")).split(",");

		for (String requete : requetes) {
			INBLeecher leecher = new INBLeecher(((String) props.get(requete
					+ ".url")), ((String) props.get(requete + ".description")),
					pc);
			List<Thread> threads = new ArrayList<Thread>();
			for (int i = 0; i < 1; i++) {
				Thread t = new Thread(leecher);
				t.setName("INBLeecher " + i);
				t.start();
				threads.add(t);
			}
			try {
				for (Thread t : threads) {
					t.join();
				}
			} catch (InterruptedException e) {
				log(e.getMessage());
				e.printStackTrace();
			}
		}
	}

}
